package com.coin.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.SysUser;
import com.coin.model.R;
import com.coin.service.SysUserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Auther: 李 力
 * @Date: 2025/5/28
 * @Description: 员工管理
 * @version: 1.0
 */
@Api(tags = "员工管理")
@RestController
@RequestMapping("/users")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * @Description: 员工分页查询
     * @Author: 李 力
     * @Date: 2025/5/28
     * @Param: current   size  mobile  fullname
     * @return: R
     */
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
            @ApiImplicitParam(name = "mobile", value = "员工的手机号码"),
            @ApiImplicitParam(name = "fullname", value = "员工的全名称"),
    })
    @PreAuthorize("hasAuthority('sys_user_query')")
    public R<Page<SysUser>> findByPage(@ApiIgnore Page<SysUser> page, String mobile, String fullname) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<SysUser> pageData = sysUserService.findByPage(page, mobile, fullname);
        return R.ok(pageData);
    }

    /**
     * @Description: 新增员工
     * @Author: 李 力
     * @Date: 2025/5/28
     * @Param: sysUser
     * @return: R
     */
    @PostMapping
    @ApiOperation(value = "新增员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUser", value = "sysUser 的json数据")
    })
    @PreAuthorize("hasAuthority('sys_user_create')")
    public R addUser(@RequestBody SysUser sysUser) {
        boolean isOk = sysUserService.addUser(sysUser);
        return isOk ? R.ok("新增成功") : R.fail("新增失败");
    }


    /**
     * @Description: 修改员工
     * @Author: 李 力
     * @Date: 2025/5/28
     * @Param: sysUser
     * @return: R
     */
    @PutMapping
    @ApiOperation(value = "修改员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUser", value = "sysUser 的json数据")
    })
    @PreAuthorize("hasAuthority('sys_user_update')")
    public R updateUser(@RequestBody SysUser sysUser) {
        boolean isOk = sysUserService.updateUser(sysUser);
        return isOk ? R.ok("修改成功") : R.fail("修改失败");
    }

    /**
     * @Description: 删除员工
     * @Author: 李 力
     * @Date: 2025/5/28
     * @Param: ids
     * @return: R
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除用户")
    @PreAuthorize("hasAuthority('sys_user_delete')")
    public R deleteUser(@RequestBody Long ids[]) {
        boolean result = sysUserService.deleteUser(ids);
        return result ? R.ok("删除成功") : R.fail("删除失败");
    }

    /**
     * @Description: 根据用户ID查询姓名
     * @Author: 李 力
     * @Date: 2025/6/9
     * @Param: userId
     * @return: R
     */
    @GetMapping("/name/{userId}")
    @PreAuthorize("hasAuthority('sys_user_query')")
    public R<String> getUserNameById(@PathVariable("userId") Long userId) {
        String userName = sysUserService.getUserNameById(userId);
        return R.ok(userName);
    }
}
